<template>
  <div>
    <el-dialog
      :title="info.openType == 'edit' ? '修改角色' : '添加角色'"
      :visible.sync="info.dialogVisible"
      width="50%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="rolename" label="角色名称">
          <el-input placeholder="请输入角色名称" v-model="ruleForm.rolename">
          </el-input>
        </el-form-item>
        <el-form-item prop="menus" label="权限">
          <!-- data:树形控件的数据 -->
          <!-- props:树形节点的配置 -->
          <!-- show-checkbox:显示选框 -->
          <!-- default-expand-all:默认展开所有项 -->
          <el-tree
            :data="treeData"
            :props="defaultProps"
            show-checkbox
            default-expand-all
            node-key="id"
            ref="tree"
            highlight-current
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="info.openType === 'edit'"
            type="primary"
            class="btn1"
            @click="submitForm"
            >立即修改</el-button
          >
          <el-button v-else type="warning" class="btn1" @click="submitForm"
            >立即添加</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getTreeDataFn, roleAddFn, roleEditFn } from "../../request";
export default {
  props: ["info"],
  data() {
    return {
      // 对话框是否显示
      dialogVisible: false,
      ruleForm: {
        rolename: "",
        menus: '',
        status: 1,
      },
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },
      // 树形控件的数据
      treeData: [],
      // 树形控件的配置
      defaultProps: {
        children: "children",
        label: "title",
      },
    };
  },
  created() {
    // 获取树形控件的数据
    this.getTreeData();
  },
  methods: {
    // 设置弹窗中的值
    setStatus(data) {
      this.ruleForm = data;
      if(typeof this.ruleForm.menus === 'string') {
        this.ruleForm.menus = this.ruleForm.menus.split(",");
      }
      // $nextTick 在dom更新完成后执行这个函数
      this.$nextTick(()=> {
        // 根据数据勾选权限,设置选中的节点
        this.$refs.tree.setCheckedKeys(this.ruleForm.menus);
      })
    },
    async getTreeData() {
      this.treeData = await getTreeDataFn();
    },
    // 提交表单
    submitForm() {
      // this.$refs.ruleForm  选择表单，目的是为了校验表单
      // validate  element自带的方法，用来校验
      this.$refs.ruleForm.validate(async (valid) => {
        // valid为真时表示校验否过
        if (valid) {
          // this.$refs.tree 选中dom节点，就是选中tree这个控件，然后操作它，获取它选中的值
          // getCheckedKeys 是element自己的方法
          this.ruleForm.menus = this.$refs.tree.getCheckedKeys();
          var res;
          if (this.info.openType === "edit") {
            // edit,是修改
            res = await roleEditFn(this.ruleForm);
          } else {
            // add，是新增
            res = await roleAddFn(this.ruleForm);
          }
          if (res === "操作成功") {
            // 关闭弹窗
            this.$emit("close");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style>
</style>